package com.xiezc.action;

import com.xiezc.dto.RespData;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import org.springframework.stereotype.Component;
import retrofit2.Call;
import retrofit2.http.Header;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import com.xiezc.util.RetrofitUtil;

import java.io.File;
import java.io.IOException;

/**
 * Created by wb-xzc291800 on 2017/6/22.
 */
@Component
public class UploadAction {

    UploadInterFace uploadInterFace = RetrofitUtil.create(UploadInterFace.class);

    public RespData upload(String ticket, File file) throws IOException {
        //构建要上传的文件
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("application/otcet-stream"), file);

        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        Call<RespData> respDataCall = uploadInterFace.upload(ticket, body);
        return respDataCall.execute().body();
    }

    interface UploadInterFace {

        @Multipart
        @POST("upload/uploadFile")
        Call<RespData> upload(@Header("ticket") String ticket,
                              @Part MultipartBody.Part file);
    }
}
